
package com.smartfit;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.util.Log;

@SuppressLint("NewApi")
public class PreferencesActivity extends PreferenceActivity
{
	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		// TODO Auto-generated method stub
		super.onCreate( savedInstanceState );
		
		getFragmentManager().beginTransaction().replace( android.R.id.content , new PreferencesFragment() ).commit();
	}

	public class PreferencesFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener
	{
		private Activity m_Activity;

		@Override
		public void onAttach( Activity activity )
		{
			// TODO Auto-generated method stub
			super.onAttach(activity);
			
			m_Activity = activity;
		}

		@Override
		public void onCreate( Bundle savedInstanceState )
		{
			// TODO Auto-generated method stub
			super.onCreate( savedInstanceState );

			addPreferencesFromResource( R.layout.preference_fragment );

			SharedPreferences pref = getPreferenceManager().getSharedPreferences();
			pref.registerOnSharedPreferenceChangeListener( this );
		}

		@Override
		public void onSharedPreferenceChanged( SharedPreferences sharedPreferences , String key )
		{
			// TODO Auto-generated method stub

			String strValue = "";
			
			if ( key.equals( "set_option_contain_gravity" ) )
			{
				boolean bIsContainedGravity = sharedPreferences.getBoolean( key , true );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetGravityContain( bIsContainedGravity );
			}
			else if ( key.equals( "set_option_apply_kalman_filter" ) )
			{
				boolean bIsAppliedKalmanFilter = sharedPreferences.getBoolean( key , true );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetKalmanFilterApply( bIsAppliedKalmanFilter );
			}
			else if ( key.equals( "set_option_apply_square" ) )
			{
				boolean bIsAppliedSquare = sharedPreferences.getBoolean( key , true );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetSquareApply( bIsAppliedSquare );
			}
			else if ( key.equals( "set_option_keep_frame" ) )
			{
				boolean bIsKeepFrame = sharedPreferences.getBoolean( key , true );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetFrameKeep( bIsKeepFrame );
			}
			else if ( key.equals( "set_option_noise_value" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetNoiseValue( Double.valueOf( strValue ) );
			}
			else if ( key.equals( "set_option_max_num_examplar" ) )
			{
				int iNumMaxExamplars = sharedPreferences.getInt( key , 3 );
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetNumMaxExamplars( iNumMaxExamplars );
			}
			else if ( key.equals( "set_option_examplar_compare_angle_threshold" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetAngleThreshold( Integer.valueOf( strValue ) );
			}			
			else if ( key.equals( "set_option_examplar_compare_distance_threshold" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetDistanceThreshold( Integer.valueOf( strValue ) );
			}
			else if ( key.equals( "set_option_enable_check_start" ) )
			{
				boolean bIsEnabledCheckStart = sharedPreferences.getBoolean( key , true );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetEnableCheckStart( bIsEnabledCheckStart );
			}
			else if ( key.equals( "set_option_check_start_constant1" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetCheckStartConstant1( Float.valueOf( strValue ) );
			}
			else if ( key.equals( "set_option_check_start_constant2" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetCheckStartConstant2( Float.valueOf( strValue ) );
			}
			else if ( key.equals( "set_option_check_start_threshold" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetCheckStartThreshold( Float.valueOf( strValue ) );
			}
			else if ( key.equals( "set_option_pattern_compare_threshold" ) )
			{
				strValue = sharedPreferences.getString( key , "" );
				
				SmartFitApp app = ( ( SmartFitApp )( m_Activity.getApplicationContext() ) );
				app.m_ExamplarDB.SetCompareThresholdForPattern( Float.valueOf( strValue ) );
			}

			Log.d( key , strValue );
		}
	}
}